package com.litchi.Service;

import com.litchi.Model.User;
import com.litchi.Param.UserParam;
import com.litchi.Repository.UserRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserRepository userRepository;

    @Override
    public List<User> findByCondition(UserParam userParam) {

        return userRepository.findAll((root, query, cb) -> {

            List<Predicate> predicates = new ArrayList<Predicate>();

            predicates.add(cb.like(root.get("userName"),"%"+userParam.getUserName()+"%"));

            return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();

        });
    }
}
